﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;

namespace SalesSys.BLL
{
    public class SupplierBLL : BaseBLL<Supplier, Guid>
    {
        public KeyValuePair<int, IEnumerable<Supplier>> Select(string searchKey, ISimplePagingInfo qinfo)
        {
            var query = Rep.Entities;
            if (!string.IsNullOrWhiteSpace(searchKey))
            {
                query = query.Where(p => p.Name.Contains(searchKey) || p.Phone.Contains(searchKey) || p.Address.Contains(searchKey));
            }
            //query = query.Where(p => p.IsDepot == isDepot);

            var count = query.Count();
            var list = query.OrderByDescending(p => p.Name).Paging(qinfo.PageNum, qinfo.PageSize).ToList();
            return new KeyValuePair<int, IEnumerable<Supplier>>(count, list);
        }
        //public IEnumerable<Supplier> Select(bool isDepot)
        //{
        //    var query = Rep.Entities;
        //    if (isDepot != null)
        //    {
        //        query = query.Where(p => p.IsDepot == isDepot);
        //    }
        //    var list = query.OrderByDescending(p => p.Name).ToList();
        //    return list;
        //}


        /// <summary>
        /// 通过名称判断钢厂信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Supplier GetSupplierByName(string name)
        {
            if (name == null)
                return null;
            Supplier supplier = Rep.Entities.FirstOrDefault(p => p.Name == name);
            return supplier;
        }

        ///// <summary>
        ///// 通过id删除驻场人员信息
        ///// </summary>
        ///// <param name="name"></param>
        ///// <returns></returns>
        //public bool DeleteEmployeeById(Guid supplierId, Guid id)
        //{
        //    var result = false;
        //    var supplier = this.Details(supplierId);
        //    if (supplier == null)
        //        return result;
        //    foreach (var emp in supplier.FieldMans)
        //    {
        //        if (emp.Id == id)
        //        {
        //            supplier.FieldMans.Remove(emp);
        //            result = true;
        //            break;
        //        }
        //    }
        //    if (result)
        //        result = this.Edit(supplier) > 0;
        //    return result;
        //}
    }
}
